package com.ruoyi.psi.purchase.storage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.purchase.order.domain.PurchaseOrder;
import com.ruoyi.psi.purchase.storage.domain.PurchaseStorage;
import com.ruoyi.psi.purchase.storage.domain.PurchaseStorageList;
import com.ruoyi.psi.purchase.storage.service.IPurchaseStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 采购订单入库
 */
@RestController
@RequestMapping("/psi/purchase/storage")
@Api(value = "采购订单入库模块", tags = "采购订单入库接口")
public class PurchaseStorageController extends BaseController {

    @Autowired
    private IPurchaseStorageService purchaseStorageService;


    /**
     * 获取采购订单入库
     * @param purchaseStorage
     * @return
     */
    @ApiOperationSupport(includeParameters = {"PurchaseStorage.purchaseStorageId", "PurchaseStorage.warehouseId", "PurchaseStorage.supplierInfoId", "PurchaseStorage.createTime"})
    @ApiOperation(value = "获取采购订单入库列表",notes = "查询所有采购订单入库")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PurchaseStorage purchaseStorage) {
        startPage();
        List<PurchaseStorage> purchaseStorages = purchaseStorageService.selectPurchaseStorageList(purchaseStorage);
        return getDataTable(purchaseStorages);
    }

    /**
     * 导出采购订单入库
     *
     * @param purchaseStorage
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody PurchaseStorage purchaseStorage) {
        List<PurchaseStorage> purchaseStorages = purchaseStorageService.selectPurchaseStorageList(purchaseStorage);
        ExcelUtil<PurchaseStorage> util = new ExcelUtil<PurchaseStorage>(PurchaseStorage.class);
        return util.exportExcel(purchaseStorages, "采购订单入库单数据");
    }

    /**
     * 采购入库新增时查询采购订单对应信息
     * @param purchaseOrder
     * @return
     */
    @PostMapping("/getPurchaseOrderList")
    public AjaxResult getPurchaseOrderList(@RequestBody PurchaseOrder purchaseOrder) {
        List<PurchaseStorageList> purchaseStorageLists = purchaseStorageService.selectPurchaseOrderListForStorage(purchaseOrder.getPurchaseOrderId());
        return AjaxResult.success(purchaseStorageLists);
    }

    /**
     * 新增采购订单入库
     * @param purchaseStorage 采购订单入库单据
     * @return
     */
    @ApiOperation(value = "新增采购订单入库", notes = "新增采购订单入库信息")
    @ApiImplicitParam(name = "purchaseStorage", value = "新增采购订单(参数：purchaseStorageId，purchaseOrderId，warehouseId，remark)", dataType = "PurchaseStorage")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PurchaseStorage purchaseStorage) {
        try {
            purchaseStorageService.insertPurchaseStorage(purchaseStorage);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新采购订单入库
     * @param purchaseStorage 采购订单入库单据
     * @return
     */
    @ApiOperation(value = "更新采购订单入库", notes = "更新采购订单入库")
    @ApiImplicitParam(name = "purchaseStorage", value = "更新采购订单入库", dataType = "PurchaseStorage")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody PurchaseStorage purchaseStorage) {
        try {
            purchaseStorageService.updatePurchaseStorage(purchaseStorage);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除采购订单入库
     * 仅修改删除标志位
     *
     * @param purchaseStorage 采购订单入库单据
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody PurchaseStorage purchaseStorage) {

        return toAjax(purchaseStorageService.updatePurchaseStorageDeleteFlag(purchaseStorage));
    }

//    /**
//     * 查看看板信息
//     *
//     * @return
//     */
//    @ApiOperation(value = "查看入库质检看板信息",notes = "查看入库质检看板信息")
//    @PostMapping("/report")
//    public AjaxResult report() {
//        return AjaxResult.success(purchaseStorageService.selectProduceForKanBan());
//    }
}
